<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSignInRecordsTable extends Migration
{
    public function up()
    {
        Schema::create('sign_in_records', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('ID');
            $table->unsignedBigInteger('employee_id')->comment('员工ID');
            $table->string('employee_name')->comment('员工姓名');
            $table->string('team_name')->comment('班组名称');
            $table->date('sign_date')->comment('签到日期');
            $table->datetime('sign_time')->comment('签到时间（提交时间）');

            // 班前危险排查（10项）
            $table->tinyInteger('has_injury')->default(0)->comment('是否有伤病：0-否 1-是');
            $table->tinyInteger('is_tired')->default(0)->comment('是否连续工作劳累：0-否 1-是');
            $table->tinyInteger('after_drinking')->default(0)->comment('是否酒后上班：0-否 1-是');
            $table->tinyInteger('is_sleepy')->default(0)->comment('是否失眠困乏：0-否 1-是');
            $table->tinyInteger('over_played')->default(0)->comment('是否玩乐过度：0-否 1-是');
            $table->tinyInteger('absent_over_10_days')->default(0)->comment('离岗时间是否超过10天：0-否 1-是');
            $table->tinyInteger('is_new_or_transferred')->default(0)->comment('是否新到岗或调岗：0-否 1-是');
            $table->tinyInteger('has_urgent_family')->default(0)->comment('家庭是否有紧急事务：0-否 1-是');
            $table->tinyInteger('had_trouble_on_way')->default(0)->comment('到岗途中是否遇到麻烦：0-否 1-是');
            $table->tinyInteger('has_social_dispute')->default(0)->comment('是否有严重社会纠葛：0-否 1-是');

            // 仪器测定（3项）
            $table->tinyInteger('drink_alcohol')->comment('是否饮酒：0-否 1-是');
            $table->integer('blood_pressure_systolic')->comment('收缩压（高压）');
            $table->integer('blood_pressure_diastolic')->comment('舒张压（低压）');
            $table->decimal('temperature', 3, 1)->comment('体温（℃）');

            $table->text('remark')->nullable()->comment('备注');
            $table->string('status', 20)->default('normal')->comment('签到状态：normal-正常 abnormal-异常');

            $table->timestamps();
            $table->softDeletes();

            $table->unique(['employee_id', 'sign_date', 'deleted_at'], 'uk_employee_date')->comment('员工每天只能签到一次');
            $table->index('sign_date', 'idx_sign_date')->comment('签到日期索引');
            $table->index(['team_name', 'sign_date'], 'idx_team_date')->comment('班组日期索引');
            $table->index('status', 'idx_status')->comment('状态索引');

            $table->foreign('employee_id', 'fk_sign_in_employee')->references('id')->on('employees')->onDelete('cascade');
        });
        DB::statement('alter table' . ' ' . config('database.connections.mysql.prefix', '') . 'feedbacks comment "签到记录表"');
    }

    public function down()
    {
        Schema::dropIfExists('sign_in_records');
    }
}

